package GameEngine;

public class GameMessage {
	
	private MessageQueue logicQueue;
	private MessageQueue guiQueue;
	
	public GameMessage(){
		logicQueue = new MessageQueue();
		guiQueue = new MessageQueue();
	}
	
	synchronized public void sendMessageToLogic(Message newMessage){
		while(guiQueue.isFull()){
			try{ 
				wait();   
			}catch (Exception e){}
		}
			
		guiQueue.enqueue(newMessage);
		notifyAll(); 
	}
	synchronized public void sendMessageToGui(Message newMessage){
		while(logicQueue.isFull()){
			try{ 
				wait();   
			}catch (Exception e){}
		}
			
		logicQueue.enqueue(newMessage);
		notifyAll(); 
	}
	synchronized public Message readMessageFromGui(){
		Message message = null;
		while(guiQueue.isEmpty()){
			try{
				wait();
			}catch (Exception e){}		
		}
				
		message = guiQueue.dequeue();
		notifyAll();

		return message;
	}
	synchronized public Message readMessageFromLogic(){
		Message message = null;
		while(logicQueue.isEmpty()){
			try{
				wait();
			}catch (Exception e){}		
		}
				
		message = logicQueue.dequeue();
		notifyAll();

		return message;
	}
	synchronized public void waitGuiConsumer(){
		while(logicQueue.isFull()){
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	synchronized public void notifyGuiHasConsumed(){
		if(logicQueue.isEmpty()){
			notifyAll();
		}
	}
}
